<template>
  <view class="tab-page group">
    <view class="nav-bar">
      <view class="nav-search">
        <uv-tabs :list="list" @click="selectTabs"></uv-tabs>
      </view>
      <view class="nav-add" @click="onCreateNewMoment()">
        <uni-icons type="camera-filled" size="26"></uni-icons>
      </view>
    </view>

    <view class="group-tip" v-if="momentList.length===0">
      还没有任何动态 ...
    </view>

    <view class="group-items" v-else>
      <scroll-view :refresher-triggered="trigger" @scroll="onPageScroll" refresher-enabled="true"
                   @refresherrefresh="refresherrefresh" class="scroll-bar" @scrolltolower="loadMore()"
                   scroll-with-animation="true"
                   scroll-y="true">
        <moment-list-item ref="fcli" :moment="momentList"></moment-list-item>
      </scroll-view>
    </view>
  </view>
</template>

<script>
import MomentListItem from "../../components/moment-list-item/moment-list-item.vue";
import UniIcons from "../../uni_modules/uni-icons/components/uni-icons/uni-icons.vue";
import http from '../../common/request';
import UvTabs from "@/uni_modules/uv-tabs/components/uv-tabs/uv-tabs.vue";

export default {
  components: {
    UvTabs,
    UniIcons,
    MomentListItem
  },
  data() {
    return {
      trigger: false,
      list: [{
        name: '最新',
      }, {
        name: '最热',
      }],
      currentCircleName: '',
      momentList: [],
      queryParams: {
        type: 0,
        pageNum: 1,
        pageSize: 10
      },
      hasNextPage: true,
    }
  },
  methods: {
    refresherrefresh() {
      this.trigger = true;
      http({
        url: '/moment/list',
        method: 'GET',
        data: this.queryParams
      }).then((res) => {
        this.momentList = res.list
        this.hasNextPage = res.hasNextPage
        this.trigger = true;
      }).finally(() => {
        this.trigger = false
      })
    },
    selectTabs(item) {
      this.queryParams.type = item.index
      this.getList()
    },
    onPageScroll(e) {
      this.$refs['fcli'].hidenTool();
    },
    onNavBack() {
      uni.switchTab({
        url: "/pages/circle/circle"
      })
    },
    onCircleInfo() {
      uni.navigateTo({
        url: "/pages/circle/circle-info?id=" + this.queryParams.circleId
      })
    },
    onCreateNewMoment() {
      uni.navigateTo({
        url: "/pages/moment/moment-add?circleId=" + this.queryParams.circleId
      })
    },

    getList() {
      http({
        url: '/moment/list',
        method: 'GET',
        data: this.queryParams
      }).then((res) => {
        this.momentList = res.list
        this.hasNextPage = res.hasNextPage
        uni.stopPullDownRefresh()
      }).finally(() => {
        uni.stopPullDownRefresh()
      })
    },

    loadMore() {
      if (!this.hasNextPage) {
        uni.showToast({
          title: `没有更多了`,
          icon: 'none',
          duration: 1500
        });
        return
      }
      this.queryParams.pageNum++
      this.queryParams.status = 'loading'
      http({
        url: '/moment/list',
        method: 'GET',
        data: this.queryParams
      }).then((res) => {
        this.momentList = this.momentList.concat(res.list)
        if (res.hasNextPage) {
          this.hasNextPage = true
        } else {
          this.hasNextPage = false
          uni.showToast({
            title: `没有更多了`,
            icon: 'none',
            duration: 1500
          });
        }
      })

    },
  },

  onShow() {
    this.queryParams.pageNum = 1
    uni.getStorage({
      key: 'last-refresh',
      success: (res) => {
        if (res.data) {
          if (Date.parse(new Date()) / 1000 - res.data > 10) {
            console.log('超过10秒刷新')
            this.getList()
            uni.setStorageSync("last-refresh", Date.parse(new Date()) / 1000);
          }
        }
      },
      fail: () => {
        console.log('第一次进入')
        this.getList()
        uni.setStorageSync("last-refresh", Date.parse(new Date()) / 1000);
      }
    });
  },
}
</script>

<style lang="scss" scoped>
.group {
  position: relative;
  display: flex;
  flex-direction: column;

  .nav-bar {
    padding: 2rpx 10rpx;
    display: flex;
    align-items: center;
    background-color: white;

    .nav-search {
      flex: 1;
    }

    .nav-add {
      cursor: pointer;
    }
  }

  .group-tip {
    position: absolute;
    top: 400rpx;
    padding: 50rpx;
    text-align: left;
    line-height: 50rpx;
    color: #10AEFF;
    font-size: 30rpx;
  }

  .group-items {
    flex: 1;
    padding: 0;
    overflow: hidden;
    position: relative;
    background-color: white;

    .scroll-bar {
      height: 100%;
    }
  }
}
</style>